package progress.exa32_1.demo;

import javax.swing.*;
import java.awt.*;


/**
 * 一个验证ActionListenerFor注解的UI面板
 */
public class ButtonFrame extends JFrame {

   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 200;

   private JPanel panel;
   private JButton yellowButton;
   private JButton blueButton;
   private JButton redButton;

   public ButtonFrame() {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
      panel = new JPanel();
      add(panel);
      yellowButton = new JButton("Yellow");
      blueButton = new JButton("Blue");
      redButton = new JButton("Red");
      panel.add(yellowButton);
      panel.add(blueButton);
      panel.add(redButton);

      //在这里使用注解处理器让被ActionListenerFor修饰的方法生效
      ActionListenerInstaller.processAnnotations(this);
   }

   /**
    * 这里使用注解方法的方式为名为yellowButton的按钮添加监听器
    */
   @ActionListenerFor(source = "yellowButton")
   public void yellowBackground() {
      panel.setBackground(Color.YELLOW);
   }

   @ActionListenerFor(source = "blueButton")
   public void blueBackground() {
      panel.setBackground(Color.BLUE);
   }

   @ActionListenerFor(source = "redButton")
   public void redBackground() {
      panel.setBackground(Color.RED);
   }
}
